home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: netcom.com!smryan
- From: smryan@netcom.com (@#$%!?!)
- Subject: Re: Return type - array of structures - please help!
- Message-ID: <smryanDpzrwu.95G@netcom.com>
- Organization: The Programmer formerly known as S M Ryan
- X-Newsreader: TIN [version 1.2 PL1]
- References: <4l0c9g$tsr@thorn.cc.usm.edu>
- Date: Wed, 17 Apr 1996 05:43:42 GMT
- Sender: smryan@netcom8.netcom.com
-
- : I wish to make the function called delete to return an array of
- : structures or if that is not possible at least create it so when
- : I pass an array of structures, it is by reference and not value like
- : listed below.
-
- (I don't know the delicate nuance of C++, so assuming this is for C,)
-
- You can't do that. The theory is the compiler must know exactly how much
- space to allocate for the return value before enterring the functions,
- thus avoiding the need to slide the value on the stack or on a secondary
- stack or whatever. Even though standard C only allows constant array
- bounds, it still doesn't know how much space to allocate. Go figure.
-
- (1) If you are always go to use fixed size arrays, wrap them in a
- struct:
-
- enum {numberStudents=...};
- typedef struct{student s[numberStudents];} Students;
-
- Students delete_1(char*,class*) {
- Students temp;
- ...temp.s[i] = ....
- return temp;
- }
-
- (2) Use pointers,
-
- typedef student *Students;
-
- Students delete_2a(char*,class*) {
- static temp[numberStudents];
- ...temp[i] =...
- return temp;
- }
- or
- Students delete_2b(char*,class*) {
- Students temp = malloc(sizeof(student)*numberStudents);
- ...temp[i] =...
- return temp;
- }
- but _never_
- Students delete_2c(char*,class*) {
- Students temp[numberStudents];
- ...temp[i] =...
- return temp;
- }
-
- In delete_1, the array size has a fixed maximum, but it can be passed
- and returned freely. Some C compilers might do strange things when returning
- structs, so check yours out. You can also assign the whole array with
- '=':
- Students class1 = class2;
-
- In delete_2a, the array size is again fixed, and every call to delete_2a
- will overwrite the results of a previous call. In delete_2b, the array size
- can be determined dynamically, and each call produces an independent result.
- The caller must take responsibility for releasing the storage by calling
- free on a result which is no longer needed. You must use element by element
- assignments or memcpy to assign the whole array.
-
- student class1[25];
- memcpy(class1,class2,n*sizeof(student));
-
- delete_2c can produce random results. The auto storage for temp in 2c is
- released when the function exits so the return value would be point to an
- undefined storage.
- --
- The Queen who loves, the Queen of life, | smryan@netcom.com PO Box 1563
- the Queen who straits, the Queen of strife;| Cupertino, California
- with gasp of death or gift of breath | (xxx)xxx-xxxx 95015
- she brings the choice of birth or knife. | I don't use no smileys
-